package com.buka.job;

import com.buka.pojo.Article;
import com.buka.service.ArticleService;
import com.buka.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class UpdateViewCountJob {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ArticleService articleService;

    @Scheduled(cron = "0 0/10 * * * ?")
    public void updateViewCountJob() {
        Map<String, Integer> viewCount = redisUtil.hGet("viewCount");
        List<Article> list = new ArrayList<>();
        for (String s : viewCount.keySet()) {
            Integer integer = viewCount.get(s);

            Article article = new Article();
            article.setId(Long.valueOf(s));
            article.setViewCount(integer.longValue());

            list.add(article);
        }

        //更新到数据库中
        articleService.updateBatchById(list);
    }

}
